#!/bin/sh
function wait_mysql(){
    while [ true ]
    do
        sleep 1
        port=`netstat -lnt | grep 3306 | wc -l`  
        pid=`ps -ef | grep mysql | grep -v grep | wc -l`
        if [ $port -eq 1 ] && [ $pid -eq 1 ]; then
            break
        fi
    done
}

if [ "$1" = "sh" ]; then
    exec "sh"
else
    if [ -f /etc/lpszoj.sit ]; then
        echo "[i] Lpszoj start..."
        exec /usr/bin/mysqld --user=root --console  &
        wait_mysql
    else
        echo "[i] Install Lpszoj... "
        MIRRORS_USTC=${MIRRORS_USTC:-""}
        if [ "$MIRRORS_USTC" != "" ]; then
            sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
            sed -i 's/#http/http/g' /etc/apk/repositories
        fi
        apk update && apk upgrade

        apk --no-cache add tzdata
        ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
        echo "Asia/Shanghai" > /etc/timezone
        apk --no-cache add php php-common php-gd php-zip php-xml php-simplexml php-mbstring php-fileinfo php-opcache php-fpm php-pdo php-pdo_mysql php-ctype php-session
        apk --no-cache add mysql mysql-client mysql-dev
        apk --no-cache add git make gcc g++ python3
        cd /app && git clone https://gitee.com/yhssdl/lpszoj.git
        mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.bak
        apk --no-cache add openjdk8

        mysql_install_db --user=root > /dev/null
        MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-""}
        if [ "$MYSQL_ROOT_PASSWORD" = "" ]; then
            MYSQL_ROOT_PASSWORD=111111
            echo "[i] MySQL root Password: $MYSQL_ROOT_PASSWORD"
        fi

        tfile=`mktemp`
        if [ ! -f "$tfile" ]; then
            return 1
        fi
        cat << EOF > $tfile
USE mysql;
FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY "$MYSQL_ROOT_PASSWORD" WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root2240881';
EOF

        echo "[i] Creating database: ojdate"
        echo "CREATE DATABASE IF NOT EXISTS \`ojdate\` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;" >> $tfile
    
        /usr/bin/mysqld --user=root --bootstrap --verbose=0 < $tfile
        rm -f $tfile
        exec /usr/bin/mysqld --user=root --console  &

        sed -i "s/post_max_size = 8M/post_max_size = 128M/g" /etc/php83/php.ini
        sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 128M/g" /etc/php83/php.ini        

        sed -i "s/'password' =>.*/'password' => '$MYSQL_ROOT_PASSWORD',/g" /app/lpszoj/config/db.php
        sed -i "s/'username' =>.*/'username' => 'root',/g" /app/lpszoj/config/db.php
        sed -i "s/OJ_PASSWORD=.*/OJ_PASSWORD=$MYSQL_ROOT_PASSWORD/g"  /app/lpszoj/judge/config.ini
        sed -i "s/OJ_USER_NAME=.*/OJ_USER_NAME=ojdate/g"  /app/lpszoj/judge/config.ini
        sed -i "s/OJ_PASSWORD=.*/OJ_PASSWORD=$MYSQL_ROOT_PASSWORD/g"  /app/lpszoj/polygon/config.ini
        sed -i "s/OJ_USER_NAME=.*/OJ_USER_NAME=ojdate/g"  /app/lpszoj/polygon/config.ini
        wait_mysql
        adduser -D -u 1536 judge
        cd /app/lpszoj
        echo -e "yes" "\n" "admin" "\n" "123456" "\n" "admin@lpszoj.org" | ./yii install
        cd /app/lpszoj/judge
        make
        cd /app/lpszoj/polygon
        make
        echo "lpszoj installation complete." > /etc/lpszoj.sit
    fi
    PHP_VERSION=`php -v>&1|awk 'NR==1{print}'|awk -F ' ' '{print $2}'|awk -F '.' '{printf "%s%s\n", $1, $2}'`
    php-fpm${PHP_VERSION}
    /app/lpszoj/judge/dispatcher -o
    /app/lpszoj/polygon/polygon
    nginx -g "daemon off;"
fi
